package com.wesley.netty.unpackage;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * UnpkgServerHandler
 *
 * @author WangPanYong
 * @since 2021/11/16 10:57
 */
public class UnpkgServerHandler extends SimpleChannelInboundHandler<CustomMessageProtocol> {
    private int count;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, CustomMessageProtocol customMessageProtocol) throws Exception {
        System.out.println("====服务端接收到消息如下====");
        System.out.println("长度=" + customMessageProtocol.getLen());
        System.out.println("内容=" + new String(customMessageProtocol.getContent(), CharsetUtil.UTF_8));

        System.out.println("服务端接收到消息包数量=" + (++this.count));
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
